﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Cash.Model;
using System.Data;
using System.Globalization;

namespace CashConnect
{
    class CashEmployerGate : ICashEmployerGate
    {
        public static CashEmployerGate Instance
        {
            get { return new CashEmployerGate(); }
        }

        public void RefreshFromDb(CashEmployer cashEmployer)
        {
            String sql =
                " SELECT se.LINK, se.F_Division, se.C_FIO, se.C_UID, sdv.C_Name1 FROM dbo.SV_Employers se " +
                " INNER JOIN dbo.SV_Divisions_View sdv ON sdv.LINK = se.F_Division where c_uid = @C_UID";
            SqlCommand cmd = new SqlCommand(sql, WFSql.DB.SqlConnection, WFSql.DB.SqlTransaction);
            WFSql.DB.AddInParameter("C_UID", SqlDbType.VarChar, cashEmployer.C_UID, cmd, false);

            cashEmployer.F_Division = "-1000";
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.Read())
                {
                    cashEmployer.F_Division += String.Format(", {0}", sdr["F_Division"]);
                    cashEmployer.C_FIO = sdr["C_FIO"].ToString();
                    cashEmployer.C_UID = sdr["C_UID"].ToString();
                    cashEmployer.LINK = sdr["LINK"].ToString();
                    cashEmployer.SvDeptName = sdr["C_Name1"].ToString();
                }
            }
        }
    }
}
